package com.fyyg.service;

import com.fyyg.common.util.StrUtil;
import com.fyyg.dal.TSysRoleMapper;
import com.fyyg.dal.TSysRoleResourceMapper;
import com.fyyg.dal.TSysUserRoleMapper;
import com.fyyg.interfaces.TSysRoleService;
import com.fyyg.model.TSysRole;
import com.fyyg.model.TSysRoleResource;
import com.fyyg.model.TSysUserRole;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("sysRoleService")
public class TSysRoleServiceImpl implements TSysRoleService{
	
	@Resource
	private TSysRoleMapper roleMapper;
	@Resource
	private TSysUserRoleMapper userRoleMapper;
	@Resource
	private TSysRoleResourceMapper roleResourceMapper;

	@Override
	public List<Map<Integer, String>> findRoleResourceListByRoleId(Integer id) {
		return roleMapper.findRoleResourceListByRoleId(id);
	}
	
	@Override
    public List<Integer> findRoleIdListByUserId(Integer userId) {
        return userRoleMapper.findRoleIdListByUserId(userId);
    }

	@Override
	public List<TSysRole> findList(String name) {
		return roleMapper.findList(name);
	}

	@Override
	public int insert(TSysRole role) {
		return roleMapper.insert(role);
	}

	@Override
	public List<TSysRole> findByName(String name) {
		return roleMapper.findByName(name);
	}

	@Override
	public int deleteByPrimaryKey(Integer id) {
		return roleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteByRoleId(Integer id) {
		return userRoleMapper.deleteByRoleId(id);
	}

	@Override
	public List<Map<String, Object>> findResourceListByRoleId(Integer roleId) {
		return roleMapper.findResourceListByRoleId(roleId);
	}

	@Override
	public void grant(Integer roleId, String resourceIds) {
		roleResourceMapper.deleteByRoleId(roleId);
		if(!StrUtil.isBlank(resourceIds)){
			String str [] = resourceIds.split("\\|");
			for (int i = 0; i < str.length; i++) {
				TSysRoleResource roleResource = new TSysRoleResource();
				roleResource.setRoleId(roleId);
				Integer resourceId = Integer.parseInt(str[i]);
				roleResource.setResourceId(resourceId);
				roleResourceMapper.insert(roleResource);
			}
		}
	}

	@Override
	public TSysRole selectByPrimaryKey(Integer id) {
		return roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(TSysRole role) {
		return roleMapper.updateByPrimaryKeySelective(role);
	}

	@Override
	public String findUserIdsByRolrId(Integer roleId) {
		return userRoleMapper.findUserIdsByRolrId(roleId);
	}

	@Override
	public List<Map<String, Object>> findUserByUserIds(
			Map<String, Object> parameterMap) {
		return roleMapper.findUserByUserIds(parameterMap);
	}

	@Override
	public int distributionUser(String ids, Integer roleId) {
		int result = 0;
		if(!StrUtil.isBlank(ids)){
			result += userRoleMapper.deleteByRoleId(roleId);
			String userIds[] = ids.split(",");
			for (int i = 0; i < userIds.length; i++) {
				Integer userId = Integer.parseInt(userIds[i]);
				TSysUserRole userRole = new TSysUserRole();
				userRole.setUserId(userId);
				userRole.setRoleId(roleId);
				result += insertUserRole(userRole);
			}
		}else{
			result += userRoleMapper.deleteByRoleId(roleId);
		}
		return result;
	}

	@Override
	public int insertUserRole(TSysUserRole userRole) {
		return userRoleMapper.insert(userRole);
	}

	@Override
	public List<TSysUserRole> findRoleListByUserId(Integer userId) {
		return userRoleMapper.findRoleListByUserId(userId);
	}
	
	
	

}
